package com.itcoon.transform.assemble.converter;

import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @author Miaoxia Yu
 * @date 2020-11-06
 */
public class AssembleConverterComposite<T,R> {

    @Autowired
    private List<AssembleConverter<T, R>> assembleConverters;

    public ConvertResult<R> convert(Type targetType, T source){
        ConvertResult<R> convertResult = new ConvertResult<>();
        for (AssembleConverter<T, R> assembleConverter : assembleConverters) {
            if (assembleConverter.support(targetType, source)) {
                convertResult.setConverted(true);
                convertResult.setConvertValue(assembleConverter.convert(targetType, source));
            }
        }
        return convertResult;
    }

    public static  class ConvertResult<R>{
        private Boolean converted;

        private R convertValue;

        public ConvertResult() {
            this.converted = false;
        }

        public Boolean getConverted() {
            return converted;
        }

        public void setConverted(Boolean converted) {
            this.converted = converted;
        }

        public R getConvertValue() {
            return convertValue;
        }

        public void setConvertValue(R convertValue) {
            this.convertValue = convertValue;
        }
    }
}
